package com.hjx.http;

import cn.hutool.core.util.ReUtil;
import org.junit.Test;

import java.util.HashMap;
import java.util.StringTokenizer;

public class HttpRequestParser {

    public static final String demo = "GET https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=wizard&rsv_spt=1&oq=Inspectors&rsv_pq=b85ab419000428ca&rsv_t=9e3arm%2BTK5pjFLnbcFOthPVen0WyVzTqga4USeefuqwO9jfQW0rhTg3c1EyUSGH%2F%2Bi%2Fp&rqlang=cn&rsv_enter=1&rsv_sug3=8&rsv_sug2=0&inputT=2618&rsv_sug4=2849 HTTP/1.1\n" +
            "Host: www.baidu.com\n" +
            "Connection: keep-alive\n" +
            "Cache-Control: max-age=0\n" +
            "Upgrade-Insecure-Requests: 1\n" +
            "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36\n" +
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\n" +
            "Accept-Encoding: gzip, deflate, br\n" +
            "Accept-Language: zh-CN,zh;q=0.9\n" +
            "Cookie: BAIDUID=3F00FD43A99C92AA1630CE6B9F885D35:FG=1; PSTM=1517291726; BIDUPSID=55D64FFCFD2E0CA364611C7DC7396849; BD_UPN=12314353; MCITY=-66%3A131%3A; ispeed_lsm=2; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDUSS=5HMzZYVXJPby03aDVXMVNpRS1DcDF3UFA5TkhjSVhLb350a0lUa1Zvd3p1c3BiQUFBQUFBJCQAAAAAAAAAAAEAAAAI9PILd29zaGlob3VqaW54aW4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADMto1szLaNbQ0; sugstore=1; delPer=0; BD_CK_SAM=1; PSINO=2; H_PS_PSSID=1443_21113_22157; H_PS_645EC=8bfbFalqRgqXbD3JrWbfMaYLF1x1BNVZ0jSZ0VYHxEOtJTJreliLyN%2F6aqkFB9TPI9F8; BDSVRTM=247\n" +
            "\n";

    public static final String javaCodeTemplate = "String url = \"%s\";\n" +
            "String result = RequestUtil.%s(url, new HashMap<String, String>(){{\n" +
            "    %s\n" +
            "}}, new HashMap<String, String>(){{\n" +
            "    %s\n" +
            "}});\n" +
            "\n" +
            "System.out.println(result);";

    public static final String jsCodeTemplate = "var url = \"%s\";\n" +
            "\n" +
            "var params = new LinkedHashMap();\n" +
            "with(Object.bindProperties({}, params)) {\n" +
            "    %s\n" +
            "}\n" +
            "var headers = new LinkedHashMap();\n" +
            "with(Object.bindProperties({}, headers)) {\n" +
            "    %s\n" +
            "}\n" +
            "var result = RequestUtil.%s(url, params, headers);\n" +
            "println(result)";

    @Test
    public void convertHttpRequest2JavaCode(){

        StringTokenizer tokenizer = new StringTokenizer(demo, "\n");
        System.out.println(tokenizer.countTokens());
        String method = "";
        String url = "";
        StringBuilder sb = new StringBuilder();
        while (tokenizer.hasMoreTokens()){
            String token = tokenizer.nextToken();
            String regex = "(GET|POST)\\s(.*)\\s(HTTP/1.1)";
            if(token.matches(regex)) {
                method = ReUtil.findAll(regex, token, 1).get(0).toLowerCase();
                url = ReUtil.findAll(regex, token, 2).get(0);
//                System.out.println();
            }
            sb.append("\t").append(tokenizer.nextToken().replaceAll(": ", "\", \"").replaceAll(".*\", \".*", "put(\"$0\");\n"));
//            System.out.println();
        }
        System.out.println(String.format(javaCodeTemplate,url, method, "", sb.toString()));
        System.out.println(String.format(jsCodeTemplate, url,"", sb.toString(), method));
    }


}
